import { OrderType } from '@/enums'
import {
  cancelOrder,
  deleteOrder,
  followOrUnfollow,
  getPrescriptionPic
} from '@/services/consult'
import { getMedicalOrderDetail } from '@/services/order'
import { sendMobileCode } from '@/services/user'
import type { Doctor, Knowledge } from '@/types/consult'
import type { OrderDetail } from '@/types/order'
import type { ConsultOrderItem } from '@/types/room'
import type { CodeType } from '@/types/user'
import {
  showFailToast,
  showImagePreview,
  showSuccessToast,
  type FormInstance,
  showToast
} from 'vant'
import { onMounted, ref, type Ref } from 'vue'

//Vue3 // 封装逻辑，规范 useXxx，表示使用某功能
export const useFollow = () => {
  const loading = ref(false)
  // {a, b} 类型，传值得时候 {a, b, c} 也可以，这是类型兼容：多的可以给少的
  const follow = async (item: Doctor | Knowledge) => {
    console.log(item.id)
    loading.value = true
    try {
      await followOrUnfollow(item.id, 'doc')
      // 点击以后已关注和取消关注取反
      item.likeFlag = item.likeFlag === 1 ? 0 : 1
    } finally {
      // 不管有没有点击成功都要关闭loading
      loading.value = false
    }
  }
  return { loading, follow }
}

//查看处方
export const showPrescription = async (id?: string) => {
  if (id) {
    const res = await getPrescriptionPic(id)
    showImagePreview([res.data.url])
  }
}

/**取消问诊订单 */

export const onCancelConsultOrder = () => {
  const loading = ref(false)
  const cancelConsultOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      await cancelOrder(item.id)
      //取消订单以后吧状态改成取消问诊订单
      item.status = OrderType.ConsultCancel
      item.statusValue = '已取消'
      showSuccessToast('取消成功')
    } catch (error) {
      showFailToast('取消订单')
    } finally {
      loading.value = false
    }
  }
  return { loading, cancelConsultOrder }
}

/**删除问诊订单 */

export const useDeleteOrder = (cd: () => void) => {
  const loading = ref(false)
  const delOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      await deleteOrder(item.id)
      showSuccessToast('删除成功')
      cd && cd()
    } catch (error) {
      showFailToast('删除失败')
    } finally {
      loading.value = false
    }
  }
  return { loading, delOrder }
}

/**药品订单详情 */

export const useOrderDetail = (id: string) => {
  const order = ref<OrderDetail>()
  const loading = ref(false)
  onMounted(async () => {
    try {
      loading.value = true
      const res = await getMedicalOrderDetail(id as string)
      order.value = res.data
    } finally {
      loading.value = false
    }
  })
  return { order, loading }
}

/**验证码composable */

export const useSendMobileCode = (mobile: Ref<string>, type: CodeType) => {
  // 设置倒计时
  const time = ref(0)
  // form
  const form = ref<FormInstance>()
  //发送短信验证码

  const onSend = async () => {
    //验证:倒计时 手机号
    if (time.value > 0) return
    // 检验手机号  name=mobile
    await form.value?.validate('mobile')
    // 校验成功发送验证码
    await sendMobileCode(mobile.value, type)
    // 开始倒计时
    time.value = 60
    showToast('发送成功')
    // eslint-disable-next-line prefer-const
    let timer: any
    //开始倒计时
    if (timer) clearInterval(timer) //如果已经有定时器了就清理定时器
    timer = setInterval(() => {
      time.value--
      // 倒计时结束清理定时器
      if (time.value <= 0) clearInterval(timer)
    }, 1000)
  }
  return { form, time, onSend }
}
